<script lang="ts" setup>
	import { onMounted, reactive,getCurrentInstance } from "vue";
	import {
		useRouter
	} from 'uni-mini-router'
	
	import {
		onShow,
		onHide,
		onLoad,
	} from '@dcloudio/uni-app'
	
	const router = useRouter()
	const {proxy} = getCurrentInstance()
	
	let data = reactive({
		tabbar: [],
		scrollTop: 0, //tab标题的滚动条位置
		current: 0, // 预设当前项的值
		menuHeight: 0, // 左边菜单的高度
		menuItemHeight: 0, // 左边菜单item的高度
	})
	const getHeightList = () => {
		console.log(456);
		// 初始化
		// let _this = this;
		let selectorQuery = uni.createSelectorQuery();
		selectorQuery.selectAll('.nav-left-item').boundingClientRect(function (rects) {
			data.leftItemHeight = rects[0].height;
			data.navLeftHeight = data.leftItemHeight * data.classifyData.length;
			data.diff = data.navLeftHeight - data.height;
		});
		selectorQuery
			.selectAll('.box')
			.boundingClientRect(function (rects) {
				let arr = [0];
				let top = 0;
				rects.forEach(function (rect) {
					// 					rect.id      // 节点的ID
					// 					rect.dataset // 节点的dataset
					// 					rect.left    // 节点的左边界坐标
					// 					rect.right   // 节点的右边界坐标
					// 					rect.top     // 节点的上边界坐标
					// 					rect.bottom  // 节点的下边界坐标
					// 					rect.width   // 节点的宽度
					// 					rect.height  // 节点的高度
					top += rect.height;
					arr.push(top);
				});
				console.log(arr);
				data.arr = arr;
			})
			.exec();
	}
	
	const getClassList = ()=>{
		proxy.$req('GET', proxy.$api.getRecruitmentTypeList).then((res) => {
			if(res.code === 200){
				data.tabbar = res.result
			}
		})
		
	}
	
	const scroll = (e) => {
		// 匹配符合左边哪个标签
		// let _this = this;
		if (data.timeoutId) {
			clearTimeout(data.timeoutId);
		}
		data.timeoutId = setTimeout(function () {
			//节流
			data.scrollHeight = e.detail.scrollTop + 1
			// + data.height / 2;
			//+1不要删除，解决最后一项某种情况下翻到底部，左边按钮并不会切换至最后一个
			//若想使切换参考线为屏幕顶部请删除 _this.height/2
			for (let i = 0; i < data.arr.length; i++) {
				let height1 = data.arr[i];
				let height2 = data.arr[i + 1];
				if (!height2 || (data.scrollHeight >= height1 && data.scrollHeight < height2)) {
					data.categoryActive = i;
					data.diff > 0 && (data.scrollLeftTop = Math.round((data.categoryActive * data.diff) / (classifyData.length - 1)));
					return false;
				}
			}
			data.categoryActive = 0;
			data.timeoutId = undefined;
		}, 10);
	}
	const categoryClickMain = (index, item) => {
		//点击某一项
		// console.log(item.name);
		// data.toView = item.name + index
		data.categoryActive = index;
		data.scrollTop == data.arr[index] ? (data.scrollTop = data.scrollTop + 1) : (data.scrollTop = data.arr[index]); //防止两次相等造成点击不触发滚动时间
	}
	const cart = (item) => {
		//点击item
		emit('itemClick', item)
		uni.showToast({
			title: item.name,
			icon: 'none'
		});
	}
	onMounted(() => {
		getClassList()
		// getHeightList();
	})
	const selectClass = (item)=>{
		let params = {
			pageType:'jobCategory',
			jobCategoryId:item.id,
			name:item.name	
		}
		item.pageType = 'jobCategory'
		router.replace({
			name:'addJob',
			params:params
		})
	}
	
	// 获取一个目标元素的高度
	const getElRect = (elClass, dataVal) =>{
		new Promise((resolve, reject) => {
			const query = uni.createSelectorQuery().in();
			query.select('.' + elClass).fields({size: true},res => {
				// 如果节点尚未生成，res值为null，循环调用执行
				if(!res) {
					setTimeout(() => {
						getElRect(elClass);
					}, 10);
					return ;
				}
				data[dataVal] = res.height;
			}).exec();
		})
	}
	
	// 点击左边的栏目切换
	const swichMenu = async(index) =>{
		if(index == data.current) return ;
		data.current = index;
		// 如果为0，意味着尚未初始化
		if(data.menuHeight == 0 || data.menuItemHeight == 0) {
			await getElRect('menu-scroll-view', 'menuHeight');
			await getElRect('u-tab-item', 'menuItemHeight');
		}
		// 将菜单菜单活动item垂直居中
		data.scrollTop = index * data.menuItemHeight + data.menuItemHeight / 2 - data.menuHeight / 2;
	}

	onLoad(() => {
		data.height = uni.getSystemInfoSync().windowHeight - data.tabBarHeight;
	})
</script>
<template>
	<view class="u-wrap">
		<view class="u-menu-wrap">
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
				<view v-for="(item,index) in data.tabbar" :key="index" class="u-tab-item" :class="[data.current==index ? 'u-tab-item-active' : '']"
				 :data-current="index" @tap.stop="swichMenu(index)">
					<text class="u-line-1">{{item.name}}</text>
				</view>
			</scroll-view>
			<block v-for="(item,index) in data.tabbar" :key="index">
				<scroll-view scroll-y class="right-box" v-if="data.current==index">
					<view class="page-view">
						<view class="class-item">
							<view class="item-container">
								<view class="thumb-box" v-for="(item1, index1) in item.children" :key="index1" @click="selectClass(item1)">
									<view class="item-menu-name">{{item1.name}}</view>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</block>
		</view>
	</view>
</template>
<style lang="scss">
	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
	}
	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}
	
	.u-search-inner {
		background-color: rgb(234, 234, 234);
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		padding: 10rpx 16rpx;
	}
	
	.u-search-text {
		font-size: 26rpx;
		color: $u-tips-color;
		margin-left: 10rpx;
	}
	
	.u-tab-view {
		width: 200rpx;
		height: 100%;
	}
	
	.u-tab-item {
		height: 110rpx;
		// background: #f6f6f6;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #444;
		font-weight: 400;
		line-height: 1;
	}
	
	.u-tab-item-active {
		position: relative;
		color: #000;
		font-size: 30rpx;
		font-weight: 600;
		background: #fff;
	}
	
	.u-tab-item-active::before {
		content: "";
		position: absolute;
		border-left: 4px solid $u-primary;
		height: 32rpx;
		left: 0;
		top: 39rpx;
	}
	
	.u-tab-view {
		height: 100%;
	}
	
	.right-box {
		background-color: rgb(250, 250, 250);
	}
	
	.page-view {
		padding: 16rpx;
	}
	
	.class-item {
		margin-bottom: 30rpx;
		background-color: #fff;
		padding: 16rpx;
		border-radius: 8rpx;
	}
	
	.item-title {
		font-size: 26rpx;
		color: $u-main-color;
		font-weight: bold;
	}
	
	.item-menu-name {
		font-weight: normal;
		font-size: 24rpx;
		color: $u-main-color;
		width:100%;
		height:100%;
		background:#f5f5f5;
		margin:0 20rpx 20rpx;
		text-align: center;
		height:60rpx;
		line-height: 60rpx;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}
	
	.item-container {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
	}
	
	.thumb-box {
		width: 48%;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		margin-top: 20rpx;
	}
	
	.item-menu-image {
		width: 120rpx;
		height: 120rpx;
	}
</style>